# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#

# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane
fastlane_require 'dotenv'

FIR_TOKEN = '329d0a0f867c11d9a81b9577c80d7b5b'

default_platform(:android)

platform :android do

  before_all do
    puts 'FASTLANE_SKIP_UPDATE_CHECK'
    puts ENV["FASTLANE_SKIP_UPDATE_CHECK"]

    if ENV["FASTLANE_SKIP_UPDATE_CHECK"] != "1"
      puts 'update fastlane'
      # update_fastlane
    end
      # increment_version_code
  end

  def removeResources()
    puts 'Remove Resources'
    sh("rm -rf ../app/src/main/res/drawable-*")
    # sh("rm -rf ../app/src/main/res/raw/app_components_echarts_components_echarts_tpl.html")
    sh("rm -rf ../app/src/main/res/raw/*")
  end

  lane :dev do
    Dotenv.load("../../.env.dev")
    build_process('nerivio-android-dev', 'assembleDevelopmentPgyerRelease')
    APK_LOCATION = "#{lane_context[SharedValues::GRADLE_APK_OUTPUT_PATH]}"
    uploadToFir(APK_LOCATION)
  end
  lane :qa do
    Dotenv.load("../../.env.qa")
    build_process('nerivio-android-qa', 'assembleQaPgyerRelease')
    APK_LOCATION = "#{lane_context[SharedValues::GRADLE_APK_OUTPUT_PATH]}"
    uploadToFir(APK_LOCATION)
  end
  lane :staging do
    Dotenv.load("../../.env.staging")
    build_process('nerivio-android-staging', 'assembleStagingPgyerRelease')
    APK_LOCATION = "#{lane_context[SharedValues::GRADLE_APK_OUTPUT_PATH]}"
    uploadToFir(APK_LOCATION)
  end
  lane :pgyer_prod do
    Dotenv.load("../../.env.production")
    build_process('nerivio-android', 'assembleProductionPgyerRelease')
    APK_LOCATION = "#{lane_context[SharedValues::GRADLE_APK_OUTPUT_PATH]}"
    uploadToFir(APK_LOCATION)
  end

  lane :all_prod_channel do
    Dotenv.load("../../.env.production")
    build_process('nerivio-android', 'assembleChannelsRelease')
    APK_LOCATION = "#{lane_context[SharedValues::GRADLE_APK_OUTPUT_PATH]}"
    APK_CHANNEL_LOCATION=APK_LOCATION.gsub("app/build/outputs/apk/productionChannel/release/", 'channel/apk/').gsub(".apk","_pgyer.apk")
    puts 'APK_CHANNEL_LOCATION'
    puts 'APK_CHANNEL_LOCATION'
    puts APK_LOCATION
    puts APK_CHANNEL_LOCATION
    uploadToFir(APK_CHANNEL_LOCATION)
  end
  lane :all_prod_auto_distribute do
    Dotenv.load("../../.env.production")
    build_process('nerivio-android', 'assembleChannelsAndUploaderRelease')
    APK_LOCATION = "#{lane_context[SharedValues::GRADLE_APK_OUTPUT_PATH]}"
    APK_CHANNEL_LOCATION=APK_LOCATION.gsub("app/build/outputs/apk/productionChannel/release/", 'channel/apk/').gsub(".apk","_pgyer.apk")
    puts 'APK_CHANNEL_LOCATION'
    puts 'APK_CHANNEL_LOCATION'
    puts APK_LOCATION
    puts APK_CHANNEL_LOCATION
    uploadToFir(APK_CHANNEL_LOCATION)
  end

  def build_process(project, target)
    others_pre_setup(project)
    gradle(task: 'clean')
    removeResources()
    gradle(task: target)
  end

  def others_pre_setup(project)
    puts project
    ENV["DELIVER_PACKAGE_VERSION"] = android_get_version_name
    ENV["DELIVER_PROJECT_NAME"] = project
    Dir.chdir('../../') do
#         sh('sed "7 s/--project.*upload-sourcemaps/--project ''$DELIVER_PROJECT_NAME'' files $App_Identifier-$DELIVER_PACKAGE_VERSION upload-sourcemaps/g" package.json > package.json.tmp && mv package.json.tmp package.json')
#         sh('sed -n 7p package.json')
    end
    Dir.chdir('..') do
#         sh('sed "3 s/defaults.project=nerivio.*/defaults.project=$DELIVER_PROJECT_NAME/g" sentry.properties > sentry.properties.tmp && mv sentry.properties.tmp sentry.properties')
#         sh('less sentry.properties')
    end
  end


  def uploadToFir(appName)
    puts 'Start to Upload'
    puts appName
    ENV["APP_BUILD_NAME"] = appName
    ENV["APP_COMMIT"] = "#{git_branch}-#{last_git_commit[:author]}-#{last_git_commit[:abbreviated_commit_hash]}-#{last_git_commit[:message]}"
    sh("fir login #{FIR_TOKEN}")
    sh('fir me')
    sh('fir publish ''$APP_BUILD_NAME'' -c=''$APP_COMMIT''')
  end

end
